package com.sec.android.app.myfiles.module.shortcut;

import android.content.Context;
import android.database.Cursor;
import android.view.View;
import android.widget.ImageView;
import android.view.ViewStub;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.info.FileType;
import com.sec.android.app.myfiles.info.MediaFile;
import com.sec.android.app.myfiles.module.abstraction.AbsFileListAdapterImp;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.module.abstraction.FileRecord.StorageType;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.provider.DbTableInfo.COLUMN_ID;
import com.sec.android.app.myfiles.thumbnail.ThumbnailImageView;
import com.sec.android.app.myfiles.thumbnail.ThumbnailMgr;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.widget.listview.FileListViewHolder;
import com.sec.android.app.myfiles.widget.listview.ListViewHolder;

public class ShortcutAdapterImp extends AbsFileListAdapterImp {

    private boolean isEnabled = true;

    public ShortcutAdapterImp(Context context, NavigationInfo navigationInfo, int layout, Cursor c, int flags) {
        super(context, navigationInfo, layout, c, flags);
    }

    @Override
    protected ListViewHolder _createViewHolder(View v) {
        ListViewHolder vh = super._createViewHolder(v);
        vh.addView(FileListViewHolder.SHORTCUT_REORDER, v.findViewById(R.id.list_reorder_stub));

        return vh;
    }

    @Override
    protected void _bindContentDescription(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        if (!getEnabled()) {
            v.setContentDescription(context.getResources().getString(R.string.folder) + ", " + record.getName() + ", "
                    + context.getResources().getString(R.string.tts_dimmed));
        } else {
            super._bindContentDescription(context, vh, v, record, position);
        }
    }

    protected void _bindView(Context context, ListViewHolder vh, View v, FileRecord record, int position) {
        super._bindView(context, vh, v, record, position);
        _bindReorderIcon(vh, v);
        View divider = v.findViewById(R.id.shortcutDivider);
        if (position == 0) {
            if (divider != null) {
                divider.setVisibility(View.GONE);
            }
        } else {
            if (divider != null) {
                divider.setVisibility(View.VISIBLE);
            }
        }

    }

    private void _bindReorderIcon(ListViewHolder vh, View v) {
        View reorderIcon = vh.getView(FileListViewHolder.SHORTCUT_REORDER);
        if (mCheckMode && reorderIcon == null) {
            reorderIcon = ensureReorderIcon(vh, v);
        }
        if (reorderIcon != null) {
            reorderIcon.setVisibility(mCheckMode ? View.VISIBLE : View.GONE);
        }
    }

    private static View ensureReorderIcon(ListViewHolder vh, View v) {
        View ret;
        ViewStub stub;
        stub = vh.getView(FileListViewHolder.SHORTCUT_REORDER, ViewStub.class);
        if (stub != null) {
            ret = stub.inflate();
        } else {
            ret = v.findViewById(R.id.reorder);
        }

        if (ret != null) {
            vh.addView(FileListViewHolder.SHORTCUT_REORDER, ret);
        }
        return ret;
    }

    @Override
    public String getSelection() {
        DbTableInfo info = ShortcutDbTableInfo.getInstance();
        StringBuilder sb = new StringBuilder();
        sb.append(info.getColumnName(DbTableInfo.COLUMN_ID.SHORTCUT_TYPE)).append('=').append(FileRecord.ShortcutType.MyFiles.ordinal());

        if (!PreferenceUtils.getShowHiddenFiles(mContext)) {
            sb.append(" AND ");
            sb.append(mTableInfo.getColumnName(COLUMN_ID.NAME)).append(" NOT LIKE '.%'");
            sb.append(" AND ");
            sb.append(mTableInfo.getColumnName(COLUMN_ID.PATH)).append(" NOT LIKE '%/.%'");
        }
        if (mNavigationInfo != null) {
            if (mNavigationInfo.isSelectOpDestination() || mNavigationInfo.isSelectDestinationPath()) {
                sb.append(" AND ").append(COLUMN_ID.FILE_TYPE).append('=').append(FileType.FOLDER);
            }
        }
        return sb.toString();
    }

    @Override
    public String[] getSelectionArgs() {
        return new String[0];
    }

    @Override
    public String getOrderBy() {
        return null;
    }

    @Override
    protected FileRecord _getFileRecord(Cursor cursor) {
        FileRecord ret = null;
        final int pathIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.PATH);
        final int nameIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.NAME);
        final int fileTypeIndex = mTableInfo.getIndex(DbTableInfo.COLUMN_ID.FILE_TYPE);

        if (cursor != null) {
            final String path = cursor.getString(pathIndex);
            final String name = cursor.getString(nameIndex);
            final int fileType = cursor.getInt(fileTypeIndex);
            ret = new ShortcutFileRecord(path, name, fileType);
        }
        return ret;
    }

    @Override
    public String getFilePath(Cursor cursor) {
        return getFilePath(cursor, mTableInfo.getIndex(COLUMN_ID.PATH));
    }

    @Override
    public StorageType getStorageType() {
        return StorageType.Shortcut;
    }

    public void setEnabled(boolean enabled) {
        isEnabled = enabled;
    }

    public boolean getEnabled() {
        return isEnabled;
    }

    @Override
    protected boolean _bindThumbnailView(Context context, ListViewHolder vh, View v, FileRecord record) {
        boolean bRet = false;

        ThumbnailImageView thumbnail = vh.getView(FileListViewHolder.THUMBNAIL, ThumbnailImageView.class);
        ImageView fileTypeIcon = vh.getView(FileListViewHolder.FILE_TYPE_ICON, ImageView.class);
        ImageView view;
        if (thumbnail != null && fileTypeIcon != null) {
            thumbnail.clearColorFilter();
            thumbnail.setThumbnailView(mNavigationInfo.getCurFragment(), record, v);
            view = ThumbnailMgr.getInstance(context).loadThumbnail(record, thumbnail, fileTypeIcon);
            if (view == null) {
                if (record.isDirectory()) {
                    UiUtils.setHomeItemIcon(context, record, thumbnail);
                } else {
                    fileTypeIcon.setImageDrawable(MediaFile.getFileTypeDrawable(context, record));
                    fileTypeIcon.setVisibility(View.VISIBLE);
                }
            }
            bRet = (view == thumbnail);
        }
        return bRet;
    }
}
